package org.jsola.contract.helper;

import org.jsola.contract.constant.AccountType;
import org.jsola.contract.constant.EnterpriseRegType;
import org.jsola.contract.constant.EnterpriseUserType;
import org.jsola.contract.constant.LegalAreaType;
import org.jsola.contract.constant.OrganType;
import org.jsola.contract.constant.PersonAreaType;
import org.jsola.contract.dto.AccountAddDTO;
import org.jsola.contract.dto.AccountUpdateDTO;
import org.jsola.contract.entity.AccountDO;
import org.jsola.contract.query.AccountQuery;
import org.jsola.contract.service.IAccountService;
import org.jsola.contract.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.contract.core.AbstractTest.SITE_ID;
import static org.jsola.user.constant.UserConstants.DEFAULT_SITE_ID;


/**
 * 账户信息
 *
 * @author JanusMix
 */
@Component
public class AccountTestHelper extends BaseTestHelper {
    
    @Autowired
    private IAccountService accountService;

    /**
     * 生成add测试数据
     * @return AccountAddDTO
     */
    public AccountAddDTO getAccountAddDTO(){
        AccountAddDTO accountAddDTO = new AccountAddDTO();
        accountAddDTO.setType(AccountType.PERSON.getValue());
        accountAddDTO.setMemberId(getRandomValue("varchar",32));
        accountAddDTO.setSource(getRandomValue("varchar",50));
        accountAddDTO.setPersonArea(PersonAreaType.MAINLAND.getValue());
        accountAddDTO.setIdNo(getRandomValue("varchar",50));
        accountAddDTO.setName(getRandomValue("varchar",255));
        accountAddDTO.setMobile(getRandomValue("varchar",45));
        accountAddDTO.setEmail(getRandomValue("varchar",255));
        accountAddDTO.setOrgan(getRandomValue("varchar",255));
        accountAddDTO.setTitle(getRandomValue("varchar",100));
        accountAddDTO.setAddress(getRandomValue("varchar",255));
        accountAddDTO.setCountry(getRandomValue("varchar",50));
        accountAddDTO.setProvince(getRandomValue("varchar",50));
        accountAddDTO.setDepartment(getRandomValue("varchar",30));
        accountAddDTO.setOrganType(OrganType.NORMAL.getValue());
        accountAddDTO.setUserType(EnterpriseUserType.AGENT.getValue());
        accountAddDTO.setOrganCode(getRandomValue("varchar",30));
        accountAddDTO.setLegalName(getRandomValue("varchar",20));
        accountAddDTO.setLegalArea(LegalAreaType.MAINLAND.getValue());
        accountAddDTO.setAgentName(getRandomValue("varchar",20));
        accountAddDTO.setAgentIdNo(getRandomValue("varchar",50));
        accountAddDTO.setScope(getRandomValue("varchar",255));
        accountAddDTO.setRegType(EnterpriseRegType.NORMAL.getValue());
        return accountAddDTO;
    }


    /**
     * 生成update测试数据
     * @return AccountUpdateDTO
     */
    public AccountUpdateDTO getAccountUpdateDTO(AccountVO accountVO){
        return accountService.selectDOById(accountVO.getId()).to(AccountUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return AccountQuery
     */
    public AccountQuery getAccountQuery() {
        AccountQuery accountQuery = new AccountQuery();
        List<AccountDO> accountDOList = accountService.list(new AccountDO(),DEFAULT_SITE_ID);
        if (!CollectionUtils.isEmpty(accountDOList)) {
            AccountDO accountDO = accountDOList.get(0);
            accountQuery = accountDO.to(AccountQuery.class);
        }
        return accountQuery;
    }

    /**
     * 保存示例
     * @return AccountVO
     */
    public AccountVO saveAccountExample(){
        AccountAddDTO addDTO = this.getAccountAddDTO();
        AccountVO accountVO = accountService.save(addDTO,buildTokenUser());
        return accountVO;
    }


}

